package com.sergiandreplace.walkshop.server.util;

import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.naming.InitialContext;
import javax.sql.DataSource;

public class DatabaseUtil {
	private static final String DB_CONTEXT = "java:comp/env/jdbc/walkshop";
    public static Connection getConnection() {
            
            try {
            InitialContext ctx = new InitialContext();
                    DataSource ds = (DataSource)ctx.lookup(DB_CONTEXT);
                    return ds.getConnection();
                    
            } catch (Exception e) {
                    System.out.println("Error opening a Connection");
                    throw new RuntimeException(e);
            }
    }
    
    public static void closeResource (Connection connection) {
            if (connection!=null) {
                    try {
                            connection.close();
                    }catch (Exception ex){ }
            }
    }
    public static void closeResource (PreparedStatement prepared) {
            if (prepared!=null) {
                    try {
                            prepared.close();
                    }catch (Exception ex){ }
            }
    }
}
